******
* author: Anthony Harding (tony.harding@gatech.edu)
* Georgia Institute of Technology, School of Public Policy


* This script creates a .csv file of country-level steady state growth for column (5) estimates
* Requires xtivreg2

* 1st Imports Data
* 2nd Runs baseline regressions
******


clear all
set mem 1G
set matsize 10000
set maxvar 10000

* Set working directory
cd "\\Client\C$\Users\aharding6\Dropbox (GaTech)\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"




*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 1. Import and prep data
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 1 - Import main data
*-------------------------------------------------------------------------------
* Import main dataset (source Burke et al. 2015) - contains temperature and growth variables
use data/input/GrowthClimateDataset, clear

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 2 - Generate new useful variables
*-------------------------------------------------------------------------------
* Generate country indicator and xtset data
xtset iso_id year



* Generate new economic variables
qui gen poor = (GDPpctile_WDIppp<50)
qui replace poor=. if GDPpctile_WDIppp==.
qui gen log_gdpCAP_wdi = log(gdpCAP_wdi)
qui gen loggdpCAP_wdi_l = l.log_gdpCAP_wdi
qui gen growthWDI_l = l.growthWDI
qui gen poor_growthWDI_l = poor*growthWDI_l

* Generate new weather variables
qui gen UDel_temp_popweight_2 = UDel_temp_popweight^2
gen poor_temp = poor*UDel_temp_popweight
gen poor_prec = poor*UDel_precip_popweight
gen poor_temp2 = poor*UDel_temp_popweight_2
gen poor_prec2 = poor*UDel_precip_popweight_2
gen dtemp = UDel_temp_popweight-l.UDel_temp_popweight
gen dprec = UDel_precip_popweight-l.UDel_precip_popweight
gen dtemp2 = UDel_temp_popweight_2-l.UDel_temp_popweight_2
gen dprec2 = UDel_precip_popweight_2-l.UDel_precip_popweight_2
gen poor_dtemp = poor*dtemp
gen poor_dprec = poor*dprec
gen poor_dtemp2 = poor*dtemp2
gen poor_dprec2 = poor*dprec2

* Generate new year variables
gen timegr = time/(1+time)
gen timegr2 = 1/2*time^2/(1+time)
*gen time2 = time^2
gen year2 = year^2
gen yeargr = year/(1+year)

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 3 - Rename and relabel key variables
*-------------------------------------------------------------------------------
* Rename variables
ren UDel_temp_popweight temp
ren UDel_temp_popweight_2 temp2
ren UDel_precip_popweight prec
ren UDel_precip_popweight_2 prec2

* Relabel variables
label variable temp "Temp"
label variable temp2 "Temp Sq."
label variable prec "Precip"
label variable prec2 "Precip Sq."
label variable dtemp "$\Delta$Temp"
label variable dtemp2 "$\Delta$Temp Sq."
label variable dprec "$\Delta$Precip"
label variable dprec2 "$\Delta$Precip Sq."
label variable poor "poor"
label variable poor_temp "Temp*Poor"
label variable poor_prec "Precip*Poor"
label variable poor_temp2 "Temp Sq.*Poor"
label variable poor_prec2 "Precip Sq.*Poor"
*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------





*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 2. Run and Store Regressions
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------


forvalues i = 1960(1)2013{
	gen year`i' = 0
	replace year`i' = 1 if year==`i'
}


levelsof iso
foreach lev in `r(levels)'{
	gen iso`lev'XXtime = 0
	replace iso`lev'XXtime = year if iso == "`lev'"
}

levelsof iso
foreach lev in `r(levels)'{
	gen iso`lev' = 0
	replace iso`lev' = 1 if iso=="`lev'"
}

*-------------------------------------------------------------------------------
* Step 5 - Pooled SR, linear country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 5 ***
eststo: xtivreg2 growthWDI temp temp2 prec prec2 (loggdpCAP_wdi_l = l3.log_gdpCAP_wdi) year1960-year2013 isoABWXXtime-isoZWEXXtime, fe

*-------------------------------------------------------------------------------
* End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 5 - Pooled LR, linear country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 5 ***
eststo: ivreg2 dtemp dtemp2 dprec dprec2 temp temp2 prec prec2 (growthWDI_l = l3.growthWDI) year1960-year2013 isoABW-isoZWE

*-------------------------------------------------------------------------------
* End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 8 - Export Regression Coefficients
*-------------------------------------------------------------------------------
* Export coefficients for use in projections
esttab using "data/output/SteadyStateGrowthPaths.csv", not noobs plain replace

* Clear results
eststo clear

*-------------------------------------------------------------------------------
* End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8
*-------------------------------------------------------------------------------

